package com.newbee.fragement.multchat;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.activity.LoginActivity;
import com.newbee.adapter.ConferenceFriendsListvewAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.listbean.FriendsInfoBean;
import com.newbee.multchat.MultChatActivity;
import com.newbee.service.MainService;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.util.MD5Util;

public class MultChatSendMailChooseFriendFragement extends Fragment implements
		OnRefrushActivity, OnClickListener, OnItemClickListener
{
	private ListView mListView;

	private View mBack;
	private List<FriendsInfoBean> mDataList;

	private ImageButton mCreate;

	private ConferenceFriendsListvewAdapter mAdapter;
	private List<String> mSelectList = new ArrayList<String>();

	private EditText mUserMail;
	private SmackClient msc;

	private TextView mEmptyview;
	private View view;

	private EditText mPassWord;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		view = inflater.inflate(R.layout.fragement_send_mail_friendslist,
				container, false);

		mEmptyview = (TextView) view.findViewById(R.id.send_empty);
		mListView = (ListView) view.findViewById(R.id.sendmail_listview);
		initLayout();
		return view;
	}

	private void initLayout()
	{
		mListView = (ListView) view.findViewById(R.id.sendmail_listview);
		mListView.setOnItemClickListener(this);
		mBack = (View) view.findViewById(R.id.mailsend_friend_choose_back);
		mBack.setOnClickListener(this);
		mCreate = (ImageButton) view
				.findViewById(R.id.conference_friends_select_sendmail);
		mCreate.setOnClickListener(this);
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.GET_FRIEND:
		{

			this.mDataList = (List<FriendsInfoBean>) paras[0];

			if (this.mDataList.size() == 0)
			{
				this.mListView.setEmptyView(mEmptyview);
			}
			else if (mAdapter == null)
			{

				mAdapter = new ConferenceFriendsListvewAdapter(mDataList,
						this.getActivity());
				this.mListView.setAdapter(mAdapter);

			}
			else
			{
				mAdapter.setList(mDataList);
				mAdapter.notifyDataSetChanged();
			}

			break;
		}
		case TaskType.CREATE_MULT_CHAT:
		{

			boolean j = (Boolean) paras[0];
			ArrayList<String> users = (ArrayList<String>) paras[2];// 带有后缀的用户名列表
			if (j)
			{

			}
			else
			{
				Toast.makeText(this.getActivity(), "创建协同录音失败！",
						Toast.LENGTH_LONG).show();
			}

			break;
		}

		default:
			break;
		}
		return null;

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
	{

		CheckBox checkBox = (CheckBox) v.findViewById(R.id.conference_checkbox);

		System.out.println("click---------------------->" + position);
		FriendsInfoBean fib = mDataList.get(position);

		if (checkBox.isChecked())
		{
			checkBox.setChecked(false);
			mSelectList.remove(fib.getEmail());
		}
		else
		{
			checkBox.setChecked(true);
			mSelectList.add(fib.getEmail());
		}

	}

	@Override
	public void onDestroy()
	{
		MainService.removeActivity(MultChatSendMailChooseFriendFragement.class
				.getName());
		super.onDestroy();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.conference_friend_choose_back:
		{
			this.getActivity().finish();
			break;
		}
		case R.id.conference_friends_select_sendmail:
		{
			int length = mSelectList.size();
			if (length <= 0)
			{
				Toast.makeText(
						MultChatSendMailChooseFriendFragement.this
								.getActivity(),
						"选择用户不能为空！", Toast.LENGTH_LONG).show();
				return;
			}
			SharedPreferences sp = this.getActivity().getSharedPreferences(
					LoginActivity.SP_USER_INFO, Activity.MODE_MULTI_PROCESS);
			final String nickName = sp.getString(LoginActivity.SP_KEY_NICKNAME,
					"");
			Intent intent = new Intent(Intent.ACTION_SENDTO);

			intent.setData(Uri.parse("mailto:"
					+ mSelectList.get(0).split("\\$")[0].replace("#", "@")));

			String[] exUsers = new String[length - 1];

			for (int i = 1; i < length; i++)
			{
				exUsers[i - 1] = mSelectList.get(i).split("\\$")[0].replace(
						"#", "@");
			}
			intent.putExtra(Intent.EXTRA_EMAIL, exUsers);

			String subject = "来自" + nickName + " " + MD5Util.getNowTime()
					+ "的录音转换文本 by 语音小助手";

			intent.putExtra(Intent.EXTRA_SUBJECT, subject);

			MultChatActivity mca = (MultChatActivity) MultChatSendMailChooseFriendFragement.this
					.getActivity();

			intent.putExtra(Intent.EXTRA_TEXT, mca.getmMultResult());// 这个地方获取要发送的文本；
			startActivity(intent);
			break;
		}
		case R.id.mailsend_friend_choose_back:
		{
			this.getActivity().finish();
			break;
		}
		default:
			break;
		}

	}
}
